package com.transsion.clouddemo.core.Custom

import android.app.Activity
import android.content.Context
import android.content.DialogInterface
import androidx.appcompat.app.AlertDialog
import com.transsion.clouddemo.R
import com.transsion.clouddemo.core.Custom.utils.ThreadUtils

class CloudDialog {

    companion object {
        private var mLoginDialog: AlertDialog? = null

        // 添加方法以传递 Context
        fun showLoginDialog(context: Context, messageResId: Int, show: DialogInterface.OnShowListener) {
            if (mLoginDialog == null) {
                val message = String.format(
                    context.getString(messageResId),
                    context.getString(R.string.app_name) // 使用传递的 context
                )
                val title = String.format(
                    context.getString(R.string.cloud_login_dialog_title),
                    context.getString(R.string.app_name) // 使用传递的 context
                )

                // 使用 AlertDialog.Builder
                mLoginDialog = AlertDialog.Builder(context)
                    .setTitle(title)
                    .setMessage(message)
                    .setPositiveButton(R.string.cloud_login_dialog_go) { dialog, _ ->
                        show.onShow(dialog)
                    }
                    .setNegativeButton(R.string.cloud_login_dialog_out) { _, _ ->
                        // 假设 context 是 Activity 类型
                        if (context is Activity) {
                            context.finish()
                        }
                    }
                    .setCancelable(true)
                    .setOnCancelListener {
                        if (context is Activity) {
                            context.finish()
                        }
                    }.create()
                mLoginDialog!!.setCanceledOnTouchOutside(false)

            }

            ThreadUtils.runOnUiThreadDelayed({
                mLoginDialog?.let {
                    if (!it.isShowing) {
                        it.show()
                    }
                }
            }, 200)
        }
    }
}
